package one.stand.service.impl;

import one.stand.model.BusinessCardVideoModel;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.request.BusinessCardVideoRequest;
import one.stand.response.BusinessCardResponse;
import one.stand.response.BusinessCardVideoResponse;
import one.stand.service.BusinessCardVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/6/21 16:48
 */
@Service
@Transactional
public class BusinessCardVideoServiceImpl extends PublicBaseService implements BusinessCardVideoService {


    @Override
    public ResultModel<Boolean> addVideo(BusinessCardVideoRequest request) {
        if (StringUtils.isBlank(request.getVideoTitle())) {
            return ResultModel.checkParamFail("videoTitle： 视频标题不能为空");
        } else if (StringUtils.isBlank(request.getVideoUrl())) {
            return ResultModel.checkParamFail("videoUrl： 视频地址不能为空");
        }

        Integer count = businessCardVideoMapper.count(new BusinessCardVideoModel() {{
            setCardId(request.getMyCardId());
        }});
        if (count >= 3) {
            return ResultModel.fail("视频数量达到上线");
        }
        BusinessCardVideoModel inModel = new BusinessCardVideoModel();
        BeanUtils.copyProperties(request, inModel);
        inModel.setCardId(request.getMyCardId());
        inModel.setCreateTime(currentDate());
        int insertRes = businessCardVideoMapper.insertSelective(inModel);
        if (insertRes <= 0) {
            return ResultModel.fail("增加用户名片视频失败");
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> addVideo(BusinessCardRequest request) {
        List<BusinessCardVideoRequest> videoRequestList = request.getVideoRequestList();
        if (null == videoRequestList || videoRequestList.size() <= 0) {
            log.info("视频数据为空，不进行插入名片视频操作。。。");
            return ResultModel.success();
        }
        //TODO 需要优化为批量插入
        videoRequestList.forEach(l -> {
            {
                l.setMyCardId(request.getMyCardId());
                addVideo(l);
            }
        });
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> uodVideo(BusinessCardVideoRequest request) {
        if (StringUtils.isBlank(request.getVideoTitle())) {
            return ResultModel.checkParamFail("videoTitle： 视频标题不能为空");
        } else if (StringUtils.isBlank(request.getVideoUrl())) {
            return ResultModel.checkParamFail("videoUrl： 视频地址不能为空");
        }else if (null == request.getVideoId()) {
            return ResultModel.checkParamFail("videoId： 视频id不能为空");
        }
        BusinessCardVideoModel inModel = new BusinessCardVideoModel();
        BeanUtils.copyProperties(request, inModel);
        int updateRes = businessCardVideoMapper.updateByPrimaryKeySelective(inModel);
        if (updateRes <= 0) {
            return ResultModel.fail("更新用户名片视频失败");
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> uodVideo(BusinessCardRequest request) {
        List<BusinessCardVideoRequest> videoRequestList = request.getVideoRequestList();
        if (null == videoRequestList || videoRequestList.size() <= 0) {
            log.info("视频数据为空，不进行修改名片视频操作。。。");
            return ResultModel.success();
        }
        //TODO 需要优化为批量插入
        videoRequestList.forEach(l -> {
            {
                l.setMyCardId(request.getMyCardId());
                uodVideo(l);
            }
        });
        return null;
    }

    @Override
    public ResultModel<Boolean> moveVideo(BusinessCardVideoRequest request) {
        if (null == request.getSorts()) {
            return ResultModel.checkParamFail("sorts: 下标数组不能为空");
        }
        List<BusinessCardVideoModel> list = businessCardVideoMapper.selectByCardList(new BusinessCardVideoModel() {{
            setCardId(request.getMyCardId());
        }});
        if (request.getSorts().length != list.size()) {
            return ResultModel.fail("排序列表数量不一致");
        }
        //TODO 需要优化成批量操作
        int i = 0;
        for (BusinessCardVideoModel outModel : list) {
            outModel.setSort(request.getSorts()[i++]);
            businessCardVideoMapper.updateByPrimaryKeySelective(outModel);
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> delVideo(BusinessCardVideoRequest request) {
        if (null == request.getVideoId()) {
            return ResultModel.checkParamFail("videoId： 视频id不能为空");
        }
        int delRes = businessCardVideoMapper.deleteByPrimaryKey(request.getVideoId());
        if (delRes <= 0) {
            return ResultModel.fail("删除视频失败");
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<List<BusinessCardVideoResponse>> moveList(BusinessCardVideoRequest request) {
        List<BusinessCardVideoModel> modelList = businessCardVideoMapper.selectByCardList(new BusinessCardVideoModel() {{
            setCardId(request.getMyCardId());
        }});
        List<BusinessCardVideoResponse> list = new ArrayList<>();
        modelList.forEach(l -> {
            {
                BusinessCardVideoResponse response = new BusinessCardVideoResponse();
                BeanUtils.copyProperties(l, response);
                list.add(response);
            }
        });
        return ResultModel.success(list);
    }

}
